package site.marqstree.smartbutler;

import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import site.marqstree.smartbutler.fragment.ButlerFragment;
import site.marqstree.smartbutler.fragment.GirlFragment;
import site.marqstree.smartbutler.fragment.UserFragment;
import site.marqstree.smartbutler.fragment.WechatFragment;
import site.marqstree.smartbutler.ui.SettingActivity;

public class MainActivity extends AppCompatActivity {

    //tab页内容
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    //tab页按钮
    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;
    //悬浮设置按钮
    @BindView(R.id.fab_setting)
    FloatingActionButton mFabSetting;
    //Title
    private List<String> mTitle;
    //页面Fragment数组
    //SparseArray:Android特有，类似Map，key为int，value为Object，比Map效率高
    private SparseArray<Fragment> mFragments = new SparseArray<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        //初始化数据
        initData();

        //初始化控件
        initView();

    }

    //初始化数据
    private void initData() {
        //初始化tab页标题
        mTitle = new ArrayList<>();
        mTitle.add(getString(R.string.text_butler_service));
        mTitle.add(getString(R.string.text_wechat));
        mTitle.add(getString(R.string.text_girl));
        mTitle.add(getString(R.string.text_user_info));
    }

    //初始化View
    private void initView() {
        //去掉阴影
        getSupportActionBar().setElevation(0);

        //默认显示首页，隐藏浮动按钮
        mFabSetting.hide();

        //初始化ViewPager
        initViewPager();
        //将ViewPager与TabLayout绑定
        mTabLayout.setupWithViewPager(mViewPager);
    }

    //初始化ViewPager
    private void initViewPager() {
        //缓存4个tab页面
        //设置VierPager页面缓存数量
        //默认为：2，包含左右两页
        mViewPager.setOffscreenPageLimit(mTitle.size());
        //设置适配器
        mViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            //选中Tab页，创建Fragment
            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        ButlerFragment butlerFragment = ButlerFragment.newInstance();
                        return butlerFragment;
                    case 1:
                        WechatFragment wechatFragment = WechatFragment.newInstance();
                        return wechatFragment;
                    case 2:
                        GirlFragment girlFragment = GirlFragment.newInstance();
                        return girlFragment;
                    case 3:
                        UserFragment userFragment = UserFragment.newInstance();
                        return userFragment;
                    default:
                        return null;
                }
            }

            //返回tab页总数
            @Override
            public int getCount() {
                return mTitle.size();
            }

            //设置标题
            @Override
            public CharSequence getPageTitle(int position) {
                return mTitle.get(position);
            }

            //销毁页面数组中的Fragment
            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                mFragments.remove(position);
                super.destroyItem(container, position, object);
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            //切换到首页时，浮动按钮隐藏
            @Override
            public void onPageSelected(int position) {
                if(position == 0){
                    mFabSetting.hide();
                }else{
                    mFabSetting.show();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //悬浮按钮点击事件
    @OnClick(R.id.fab_setting)
    public void onViewClicked(View view) {
        //触发Bugly上报
        //CrashReport.testJavaCrash();
        //启动配置页面
        startActivity(new Intent(this, SettingActivity.class));
    }
}
